package com.oa.lms.apps.business.user.impl;

import com.oa.lms.apps.business.user.IMailInfoService;
import com.oa.lms.apps.constants.IMailStatus;
import com.oa.lms.apps.dao.user.IMailInfoDao;
import com.oa.lms.apps.entity.user.MailInfoValue;

public class MailInfoServiceImpl implements IMailInfoService {
	
	private IMailInfoDao mailInfoDao;
	
	private MailInfoValue mailInfoValue;

	@Override
	public Integer add(String mailBox) {
		mailInfoValue.setMailBox(mailBox);
		mailInfoValue.setVerifyCode(this.getVerifyCode());
		mailInfoValue.setStatus(IMailStatus.INACTIVE);
		return mailInfoDao.add(mailInfoValue);
	}

	@Override
	public void modify(Integer mailId, String mailBox) {
		mailInfoValue = this.queryByMailId(mailId);
		mailInfoValue.setMailBox(mailBox);
		mailInfoValue.setVerifyCode(getVerifyCode());
		mailInfoValue.setStatus(IMailStatus.INACTIVE);
		mailInfoDao.modify(mailInfoValue);
	}
	
	@Override
	public void activeMailBox(Integer mailId) {
		mailInfoValue = this.queryByMailId(mailId);
		if(mailInfoValue != null){
			mailInfoValue.setStatus(IMailStatus.ACTIVE);
			mailInfoValue.setVerifyCode(getVerifyCode());
			mailInfoDao.modify(mailInfoValue);
		}
		
	}

	@Override
	public MailInfoValue queryByMailId(Integer mailId) {
		return mailInfoDao.queryByMailId(mailId);
	}

	@Override
	public MailInfoValue queryByMailbox(String mailBox) {
		return mailInfoDao.queryByMailbox(mailBox);
	}
	
	private String getVerifyCode() {
		// 随机产生一个16位的验证码
		String sourcenum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String siglenum = "";
		// 该变量保存验证码
		String verifyCode = "";
		for (int i = 0; i < 16; i++) {
			int index = (int) (Math.random() * 100) % 61;
			siglenum = sourcenum.substring(index, index + 1);
			verifyCode += siglenum;
		}
		return verifyCode;
	}
	
	
	//Getter and Setter

	public IMailInfoDao getMailInfoDao() {
		return mailInfoDao;
	}

	public void setMailInfoDao(IMailInfoDao mailInfoDao) {
		this.mailInfoDao = mailInfoDao;
	}

	public MailInfoValue getMailInfoValue() {
		return mailInfoValue;
	}

	public void setMailInfoValue(MailInfoValue mailInfoValue) {
		this.mailInfoValue = mailInfoValue;
	}

}
